package com.atlin.project.service.impl;


import com.atlin.project.contant.CommonConstant;
import com.atlin.project.model.domain.BiProducts;
import com.atlin.project.model.dto.biProduces.BiProductsQueryRequest;
import com.atlin.project.model.vo.biProduces.BiProductsVO;
import com.atlin.project.service.BiProductsService;
import com.atlin.project.mapper.BiProductsMapper;
import com.atlin.project.utils.sql.SqlUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author atlin
 * @description 针对表【bi_products(BI商品表)】的数据库操作Service实现
 * @createDate 2024-11-25 16:25:56
 */
@Service
public class BiProductsServiceImpl extends ServiceImpl<BiProductsMapper, BiProducts>
        implements BiProductsService {

    @Override
    public void valid(BiProducts biProducts, boolean add) {

    }

    @Override
    public BiProductsVO getBiProductsVO(BiProducts biProducts, HttpServletRequest request) {
        return BiProductsVO.objToVo(biProducts);
    }

    /**
     * 获取查询包装类（用户根据哪些字段查询，根据前端传来的请求对象，得到 mybatis 框架支持的查询 QueryWrapper 类）
     *
     * @param biProductsQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<BiProducts> getQueryWrapper(BiProductsQueryRequest biProductsQueryRequest) {
        QueryWrapper<BiProducts> queryWrapper = new QueryWrapper<>();
        if (biProductsQueryRequest == null) {
            return queryWrapper;
        }
        Long id = biProductsQueryRequest.getId();
        String sortField = biProductsQueryRequest.getSortField();
        String sortOrder = biProductsQueryRequest.getSortOrder();
        String name = biProductsQueryRequest.getName();
        String category = biProductsQueryRequest.getCategory();
        BigDecimal price = biProductsQueryRequest.getPrice();
        String description = biProductsQueryRequest.getDescription();
        Integer status = biProductsQueryRequest.getStatus();
        
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.like(StringUtils.isNotBlank(category), "category", category);
        queryWrapper.like(StringUtils.isNotBlank(description), "description", description);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(status), "status", status);
        queryWrapper.eq(price != null && price.compareTo(BigDecimal.ZERO) > 0, "price", price);
        
        queryWrapper.eq("isDelete", false);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);
        
        return queryWrapper;
    }

    @Override
    public Page<BiProductsVO> getBiProductsVOPage(Page<BiProducts> biProductsPage, HttpServletRequest request) {
        List<BiProducts> biProductsList = biProductsPage.getRecords();
        Page<BiProductsVO> biProductsVOPage = new Page<>(biProductsPage.getCurrent(), biProductsPage.getSize(), biProductsPage.getTotal());
        if (CollectionUtils.isEmpty(biProductsList)) {
            return biProductsVOPage;
        }

        // 填充信息
        List<BiProductsVO> biProductsVOList = biProductsList.stream().map(BiProductsVO::objToVo).collect(Collectors.toList());
        biProductsVOPage.setRecords(biProductsVOList);
        return biProductsVOPage;
    }
}




